package com.zc.auth.security.core.configuration;

import com.zc.auth.security.core.access.meta.DefaultUrlRoleMetaDataManager;
import com.zc.auth.security.core.access.meta.RedisUrlRoleMetaDataManager;
import com.zc.auth.security.core.access.meta.UrlRoleMetaDataManager;
import com.zc.auth.security.core.properties.SecurityProcessorProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 接口角色权限自动配置
 *
 * @author zcj
 * @version 1.0.0
 * @date 2022/1/21 10:53
 */
@Configuration
@ConditionalOnMissingBean({UrlRoleMetaDataManager.class})
//@ConditionalOnSingleCandidate(DataSource.class)
@EnableConfigurationProperties({SecurityProcessorProperties.class})
public class RoleAuthMetaDataConfiguration {

    @Autowired
    private SecurityProcessorProperties properties;

    @Bean
    @ConditionalOnMissingBean(value = UrlRoleMetaDataManager.class)
    public UrlRoleMetaDataManager defaultUrlRoleMetaDataManager() {
        if ("redis".equals(properties.getRoleCacheType())) {
            return new RedisUrlRoleMetaDataManager(this.properties.getServerName());
        }
        return new DefaultUrlRoleMetaDataManager(properties.getServerName());
    }

    @Bean
    @ConditionalOnMissingBean(CacheManager.class)
    @ConditionalOnProperty(name = "spring.cache.type", havingValue = "jcache")
    public CacheManager jCacheCacheManager() {
        return new JCacheCacheManager();
    }

}
